package com.jason.kotlin.meta.architecture2.ui.utils

import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.google.android.material.tabs.TabLayout
import com.jason.kotlin.meta.R

object MainBottomNav {
    //Tab 文字
    private val MAIN_BOTTOM_TAB_TITLES = intArrayOf(
        R.string.wan_home_title,
        R.string.wan_project_title,
        R.string.wan_square_title,
        R.string.wan_public_title,
        R.string.wan_my_title
    )

    //Tab 图片
    private val MAIN_BOTTOM_TAB_IMAGES = intArrayOf(
        R.drawable.icon_menu_home,
        R.drawable.icon_menu_project,
        R.drawable.icon_menu_square,
        R.drawable.icon_menu_public,
        R.drawable.icon_menu_my
    )

    fun setBottomTabs(tabLayout: TabLayout, inflater: LayoutInflater) {
        for (i in MAIN_BOTTOM_TAB_TITLES.indices) {
            val tab = tabLayout.newTab()
            val view: View = inflater.inflate(R.layout.main_tab_item, null)
            tab.customView = view
            setBottomTabInfo(view, i)
            tabLayout.addTab(tab)
        }
    }

    /**
     * 重置Tab View值
     * @param tabLayout
     */
    fun resetBottomTab(tabLayout: TabLayout?) {
        if (tabLayout != null && tabLayout.tabCount > 0) {
            val count = tabLayout.tabCount
            for (i in 0 until count) {
                val tab = tabLayout.getTabAt(i)
                val view = tab!!.customView
                setBottomTabInfo(view, i)
            }
        }
    }

    /**
     * 填充底部tab信息
     * @param view
     * @param index
     */
    private fun setBottomTabInfo(view: View?, index: Int) {
        val tvTitle = view!!.findViewById<TextView>(R.id.tv_main_bottom_tab_title)
        tvTitle.setText(MAIN_BOTTOM_TAB_TITLES[index])
        val imgTab =
            view.findViewById<ImageView>(R.id.iv_main_bottom_tab_icon)
        imgTab.setImageResource(MAIN_BOTTOM_TAB_IMAGES[index])
        val tvTip = view.findViewById<TextView>(R.id.tv_tip)
        tvTip.visibility = View.VISIBLE
    }


}